<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">

		<el-container v-loading="loading">

			<el-main style="padding:0 20px 20px 20px">

				<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">

					<el-form-item label="活动类型" prop="promo_type">
						<el-radio-group v-model="form.promo_type">
							<el-radio :label="'quantity'">限量促销</el-radio>
							对一定数量商品进行打折销售，售卖完毕后恢复原价
							<el-radio :label="'time'">限时促销</el-radio>
						</el-radio-group>
					</el-form-item>

					<el-form-item label="活动名称" prop="name">
						<el-input v-model="form.promo_name" placeholder="请输入活动名称(客户端不展示)" clearable></el-input>
					</el-form-item>

					<el-form-item label="活动时间" prop="promo_time_range" v-if="form.promo_type=='time'">
						<el-date-picker v-model="form.promo_time_range" type="datetimerange" align="right" unlink-panels
							range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期"
							value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime">
						</el-date-picker>
					</el-form-item>

					<el-form-item label="活动策略" prop="x" v-if="false">

						<el-input placeholder="x" v-model="form.total_purchase_limit">
							<template v-slot:prepend>
								每次最多可购买
							</template>
							<template v-slot:append>
								个
							</template>
						</el-input>

						<el-input placeholder="x" v-model="form.cycle_days">
							<template v-slot:prepend>
								在
							</template>
							<template v-slot:append>
								天内
							</template>
						</el-input>

						<el-input placeholder="x" v-model="form.cycle_count">
							<template v-slot:prepend>
								循环
							</template>
							<template v-slot:append>
								次
							</template>
						</el-input>

					</el-form-item>

					<el-form-item label="备注" prop="promo_remark">
						<el-input type="textarea" v-model="form.remark" placeholder="备注也不在前台展示" clearable></el-input>
					</el-form-item>

					<el-form-item label="选择商品" prop="promo_skus">
						<el-button type="primary" icon="el-icon-plus">选择商品</el-button>
					</el-form-item>

				</el-form>

			</el-main>
		</el-container>

		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show' && !loading" type="primary" :loading="isSaveing" @click="submit()">保
				存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				defaultTime: [
					new Date(2000, 1, 1, 0, 0, 0),
					new Date(2000, 2, 1, 23, 59, 59),
				],
				loading: true,
				mode: "add",
				titleMap: {
					add: '新增活动',
					edit: '编辑活动',
					show: '查看活动'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					promo_time_range: '',
					promo_type: 'quantity',
					name: "",
					remark: "",
					cycle_days: null,
					cycle_count: null,
					total_purchase_limit: null,
					not_before: null
				},
				//验证规则
				rules: {
					not_before: [{
						required: true,
						message: '选择活动开始日期'
					}],
					name: [{
						required: true,
						message: '输入活动名称'
					}]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				this.loading = false;
				return this
			},
			//加载树数据
			async getGroup() {
				var res = await this.$API.system.dept.list.get();
				this.groups = res.data;
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == 'add') {
							res = await this.$API.system.promotion.create.post(this.form);
						} else {
							res = await this.$API.system.promotion.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true
				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.promotion.info.get(params)
					this.form = Object.assign(this.form, res.data)
					this.loading = false
				}, 400)
			}
		}
	}
</script>

<style>
</style>
